
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
<link rel="stylesheet" href="{% static 'plugins/bootstrap-3.4.1/css/bootstrap.min.css' %}">

    <style>
       .account h2 {
            margin-top: 10px;
            text-align: center;
        }
        .error-msg{
            color:red;
            height:9px;
            font-size: 13px;
        }
        .glass-container{
            width: 400px;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 20px;
            border-radius: 0;
            backdrop-filter: blur(0px);
            background-color: rgba(0,191,255, 0);
            box-shadow: rgba(0, 0, 0, 0.3) 2px 8px 8px;
            border: 0 rgba(255,255,255,0.4) solid;
            border-bottom: 0 rgba(40,40,40,0.35) solid;
            border-right: 0 rgba(40,40,40,0.35) solid;

            margin-left: auto;
            margin-right: auto;
            margin-top: 20px;
            padding: 20px 40px;
            color: black;
            font-size: 1.5em;
        }
        body{
            background-image: url("{% static "images/bg_login1.jpg" %}");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            height: 100vh;
            margin: 0;
            padding: 0;
        }
        a{
            color: white;
            font-size: 0.9em;
        }
        h2{
            text-align: center;
        }
        .btn {
	        background: white;
            color: black;
            width: 8em;
        }

        .btn:hover{
            background: black;
            color: white;
        }
        a:hover{
            color: #afd9ee;
        }

    </style>

</head>

<nav class="navbar navbar-default">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#"> 二手交易平台 </a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="/item/">返回</a></li>
            </ul>
        </div>
    </div>
</nav>


<body>
<div class="glass-container" id="glass">
    <form method="post" novalidate>
     <h2>用户注册</h2>
        {% csrf_token %}
        {% for field in form %}
            {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>

                        <div class="clearfix">
                            <div class="col-md-6" style="padding-left: 0;">{{ field }}<div class="error-msg">{{ field.errors.0 }}</div></div>
                            <div class="col-md-6"><input type="button" id="btnSms" class="btn btn-default "
                                                         value="获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
            <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                    <div class="error-msg">{{ field.errors.0 }}</div>
                    </div>
            {% endif %}
        {% endfor %}


        <div style="text-align: center">
            <input id="btnSubmit" type="submit" class="btn btn-primary" value="注  册"/>
        </div>
        <div style="text-align: center; font-size: 1em; margin-top: 1em;">
                <a href="{% url 'login_sms' %}">短信登录</a>
                <a href="{% url 'login' %}">密码登录</a>
        </div>
    </form>
</div>

<script src="{% static 'js/jquery-3.6.0.min.js' %}"></script>
<script src="{% static 'plugins/bootstrap-3.4.1/js/bootstrap.min.js' %}"></script>
    <script>
    btnSms_click()
    btnSubmit_click()


      function btnSms_click(){
 $('#btnSms').click(function(){
        $('.error-msg').empty();
     {#获取输入的手机号码#}
     var mobile_phone=$('#id_mobile_phone').val();
console.log(mobile_phone)
     $.ajax({
         {#请求的路由地址#}
         url:"/user/sms/code/",
         type:"GET",
         data:{
            mobile_phone:mobile_phone,
             tpl:"register",
         },
         dataType:"JSON",
         success:function(res){
             if (res.status) {
          sendSmsRemind(); }
             else {
 // 错误信息
  console.log(res.err);
                 // {status:False, error:{ mobile_phone: ["错误信 息"，],code: ["错误信息"，] } }
    $.each(res.error, function (key, value) {
    {#key：字段名#}
    $("#id_" + key).next().text(value[0]);
      }) }
         }

     })


 })
    }
function sendSmsRemind() {
 var $smsBtn = $('#btnSms');
 $smsBtn.prop('disabled', true); // 禁⽤
 var time = 60;
 var remind = setInterval(function () {
 $smsBtn.val(time + '秒重新发送');
 time = time - 1;
 if (time < 1) {
 clearInterval(remind);
 $smsBtn.val('点击获取验证码').prop('disabled', false);
 }
 }, 1000) }
function btnSubmit_click() {
 $('#btnSubmit').click(function () {
 // 每次提交的时候如果先清空错误信息
     console.log('?')
 $('.error-msg').empty();
 // 收集表单中的数据（找到每⼀个字段）$('#regForm').serialize()
 // 数据ajax发送到后台
 $.ajax({
 url: "/user/register/",
 type: "POST",
 data: $('#regForm').serialize(), // 所有字段数据 + csrf token
 dataType: "JSON",
 success: function (res)
 {
     console.log(res)
 if (res.status) {
     console.log('res.data')
 // location.href = res.data;
 } else {
 $.each(res.error, function (key, value) {
 $("#id_" + key).next().text(value[0]);
 })
 }
 }
 })
 }) }
    </script>
</body>
</html>
